package cn.ssm.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.naming.spi.DirStateFactory.Result;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.apache.naming.resources.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.ssm.pojo.Admin;
import cn.ssm.pojo.User;
import cn.ssm.service.UserService;
import cn.ssm.util.SmsDemo;

@Controller
@RequestMapping("/user")
@MultipartConfig
public class UserController {
	@Autowired
	UserService userService;
	
	@RequestMapping("/getUserList")
	@ResponseBody
	public Object getUserList() {
		List<User> user=userService.getUserList();
		return user;
		
	}
	
	/**
	 * 查询所有用户信息
	 * @return
	 */
	@RequestMapping("/getAllUserList")
	@ResponseBody
	public Object getAdminList(Integer nowPage, Integer size) {
		//接受的参数
		Map<String, Object> paramas = new HashMap<String, Object>();
		if(nowPage !=null || size !=null){
			paramas.put("start", (nowPage-1)*size);
			paramas.put("size", size);
		}
		
		//返回的结果
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		
		try{
			List<User> allUserList = userService.getAllUserList(paramas);
			map.put("result", allUserList);
			map.put("message", "查询用户成功");
			map.put("success", true);
		}catch(Exception e){
			map.put("result", "查询用户出现异常");
		}
		return map;
	}
	
	//查询用户总数
	@RequestMapping("/getUserCount")
	@ResponseBody
	public int getUserCount(){
		return userService.getUserCount();
	}
	
	//删除用户
	@RequestMapping("/deleteUserList")
	@ResponseBody
	public int deleteUserList(@Param("id")Integer id){
		
		return userService.deleteUserList(id);
		
	}
	
	
	//注册
	@RequestMapping("/addUser")
	@ResponseBody
	public Object addUser(@Param("userName")String userName, 
			@Param("password")String password){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("success",false);
		int count=userService.addUser(userName,password);
		try {
			map.put("message", "注册成功");
			map.put("success", true);

			
		} catch (Exception e) {
			map.put("result", "注册用户出现异常");
			e.printStackTrace();
		}
		
		return map;
	}
	
	/**
	 * 根据username查昵称nickName
	 * @param phone
	 * @return
	 */
	@RequestMapping("/getUserListByUserName.htm")
	@ResponseBody
	public Object getUserListByUserName(String phone) {

		// 返回值
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		
		try {
			List<User> user=userService.getUserListByUserName(phone);
			map.put("result", user);
			map.put("message", "查询用户信息成功！");
			map.put("success", true);
		} catch (Exception e) {
			map.put("message", "查询用户信息出现异常！");
			e.printStackTrace();
		}
		return map;
	}
	
	//注册
	@RequestMapping("/addUsers")
	@ResponseBody
	public Object addUsers(User user){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("success",false);
		try {
			if (user != null) {
				if(user.getSex()==0||user.getSex()==1){
					user.setSex(1);
					user.setNickName("帅哥");
				}
				if(user.getSex()==2){
					user.setNickName("美女");
				}
				int count = userService.addUsers(user);
				if (count > 0) {
					map.put("success", true);
					map.put("message", "注册成功");
				}
			} 
		} catch (Exception e) {
			map.put("result", "添加用户出现异常");
			e.printStackTrace();
		}
		return map;
	}
	
	//验证注册
	@RequestMapping("/selectUser")
	@ResponseBody
	public Object selectUser(User user){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("success",false);
		try {
			if (user != null) {
				List<User> users = userService.selectUser(user);
				if (users.isEmpty()) {
					map.put("success", true);
					map.put("message", "账号密码可以注册");
				}else{
					map.put("message", "已注册过，请更换");
				}
			} 
		} catch (Exception e) {
			map.put("result", "添加用户出现异常");
			e.printStackTrace();
		}
		return map;
	}
	
	
	/**
	 * 发送验证码
	 * @param phone
	 * @return
	 */
	@RequestMapping("/sendMsg.htm")
	@ResponseBody
	public Object sendMsg(@Param("phone")String phone) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		map.put("message", "发送验证码成功!");
     	String code=String.valueOf(new Random().nextInt(899999) + 100000);
		try {
			SmsDemo.sendSms(phone,code);
			map.put("success", true);
			map.put("code", code);
		} catch (Exception e) {
			map.put("message", "发送验证码异常!");
		}
		return map;
		
	}
	
	
	
	
	/**
	 * 发送取票编码给用户 
	 * @param phone
	 * @return
	 */
	@RequestMapping("/sendCode.htm")
	@ResponseBody
	public Object sendCode(@Param("phone")String phone,@Param("code")String code) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		map.put("message", "发送验证码成功!");
		try {
			SmsDemo.sendSms1(phone,code);
			map.put("success", true);
			map.put("code", code);
			
		} catch (Exception e) {
			map.put("message", "发送验证码异常!");
		}
		return map;
		
	}
	
	/**
	 * 上传图片
	 * @param phone
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping("/upload.htm")
	@ResponseBody
	public Object upload(@RequestParam("file")MultipartFile file,HttpSession session) throws IllegalStateException, IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		if (!file.isEmpty()) {
           //上传文件路径
           String path = session.getServletContext().getRealPath("/static/img/");
           //上传文件名
           String filename = file.getOriginalFilename();
           File filepath = new File(path, filename);

           //判断路径是否存在，没有就创建一个
           if (!filepath.getParentFile().exists()) {
               filepath.getParentFile().mkdirs();
           }
           
	         //将上传文件保存到一个目标文档中
	         File file1 = new File(path + File.separator + filename);
	         file.transferTo(file1);//存图片
	         map.put("file", "../static/img/"+filename);
	         return map;
	     }
		return null; 
	}
	
	
	/**
	 * 保存个人资料到数据库
	 * @param phone
	 * @return
	 */
	@RequestMapping("/savePic.htm")
	@ResponseBody
	public Object savePic(@Param("newPhone")String newPhone,@Param("nickName")String nickName,
						@Param("url")String url,@Param("oldPhone")String oldPhone) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		map.put("newPhone", newPhone);
		map.put("nickName", nickName);
		map.put("url", url);
		map.put("oldPhone", oldPhone);
		System.out.println(newPhone+"/"+nickName+"/"+url+"/"+oldPhone);
		int count = userService.updatePic(map);
		if(count>0){
			System.out.println("成功");
		}else{
			System.out.println("失败");
			
		}
		try {
			map.put("message", "保存成功！");
			map.put("success", true);
			
		} catch (Exception e) {
			map.put("message", "保存失败！");
			e.printStackTrace();
		}
		return map;
	}
	
	//查询手机号是否存在
		@RequestMapping("/selectPhone")
		@ResponseBody
		public Object selectPhone(@Param("phone")String phone){
			Map<String, Object> map = new HashMap<String,Object>();
			map.put("success",false);
			User user=userService.selectPhone(phone);
			System.out.println(user);
			try {
				if(user==null){
					map.put("success", true);
					map.put("message","此号码可用！");
				}else{
					map.put("success", false);
					map.put("message","此号码已存在，请更换新号码！");
				}
			} catch (Exception e) {
				map.put("result", "手机号查询出现异常");
				e.printStackTrace();
			}
			
			return map;
		}
	
	
}
